/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pig4cloud.pig.common.security.component;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authorization.method.PrePostTemplateDefaults;
import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;

/**
 * 资源服务器自动配置类
 * <p>
 * 配置资源服务器所需的各种组件，包括权限服务、Token提取器、异常处理器等
 * </p>
 *
 * @author lengleng
 * @date 2025/05/31
 */
@RequiredArgsConstructor
@EnableConfigurationProperties(PermitAllUrlProperties.class)
public class PigResourceServerAutoConfiguration {

	/**
	 * 配置权限校验服务
	 * <p>
	 * 注册名为"pms"的Bean，用于在@PreAuthorize注解中使用，如：@PreAuthorize("#pms.hasPermission('sys_user_add')")
	 * </p>
	 * @return 权限服务实例
	 */
	@Bean("pms")
	public PermissionService permissionService() {
		return new PermissionService();
	}

	/**
	 * 配置Bearer Token提取器
	 * <p>
	 * 用于从HTTP请求中提取Bearer Token，支持从请求头和请求参数中获取
	 * </p>
	 * @param urlProperties 放行的URL配置属性
	 * @return Token提取器实例
	 */
	@Bean
	public PigBearerTokenExtractor pigBearerTokenExtractor(PermitAllUrlProperties urlProperties) {
		return new PigBearerTokenExtractor(urlProperties);
	}

	/**
	 * 配置资源服务器认证异常处理入口点
	 * <p>
	 * 处理资源服务器的认证异常，返回统一格式的错误响应
	 * </p>
	 * @param objectMapper Jackson对象映射器，用于序列化响应
	 * @param securityMessageSource 安全模块的国际化消息源
	 * @return 认证异常处理入口点实例
	 */
	@Bean
	public ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint(ObjectMapper objectMapper,
			MessageSource securityMessageSource) {
		return new ResourceAuthExceptionEntryPoint(objectMapper, securityMessageSource);
	}

	/**
	 * 配置不透明Token内省器
	 * <p>
	 * 用于验证和解析不透明的访问令牌，从授权服务中获取令牌的详细信息
	 * </p>
	 * @param authorizationService OAuth2授权服务，用于查询令牌信息
	 * @return Token内省器实例
	 */
	@Bean
	public OpaqueTokenIntrospector opaqueTokenIntrospector(OAuth2AuthorizationService authorizationService) {
		return new PigCustomOpaqueTokenIntrospector(authorizationService);
	}

	/**
	 * 配置注解模板表达式默认值
	 * <p>
	 * 支持在@PreAuthorize、@PostAuthorize等注解中使用自定义的表达式模板
	 * </p>
	 * @return 注解模板表达式默认值配置
	 */
	@Bean
	AnnotationTemplateExpressionDefaults prePostTemplateDefaults() {
		return new AnnotationTemplateExpressionDefaults();
	}

}
